package com.bilibili.lzx.service.handler;

import com.bilibili.lzx.dao.domain.JsonResponse;
import com.bilibili.lzx.dao.domain.exception.ConditionException;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Order(1)
public class CommonGlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public JsonResponse<String> commonExceptionHandler(Exception e, HttpServletRequest request) {
        String message = e.getMessage();
        if (e instanceof ConditionException) {
            ConditionException ce = (ConditionException) e;
            e.printStackTrace();
            return JsonResponse.fail(message,ce.getCode());
        }
        e.printStackTrace();
        return JsonResponse.fail(message,"500");
    }
}
